package com.shzq.controller;

import com.shzq.pojo.ResultDTO;
import com.shzq.pojo.SuggestDO;
import com.shzq.service.SuggestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zml
 * time: 2020/5/7 17:07
 * mailbox  zmlelselove@126.com
 */
@RestController
@RequestMapping("/suggest")
public class SuggestController {

    @Autowired
    private SuggestService suggestService;

    @PostMapping
    public ResultDTO add(@RequestBody SuggestDO suggest, HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String uid = null;
        for (Cookie cookie : cookies) {
            if("uid".equals(cookie.getName())){
                uid = cookie.getValue();
            }
        }
        if(uid!=null){
            suggest.setUsername(uid);
            try {
                suggestService.suggest(suggest);
                return new ResultDTO("建议提交成功，谢谢你宝贵的建议","success",ResultDTO.SUCCESS);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return new ResultDTO("建议提交失败","warning",ResultDTO.WITH_OUT_LOGIN);
    }

}
